/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import Utils.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author aleX
 */
@WebServlet(name = "Servlet_Segreteria", urlPatterns = {"/Corsi_Segreteria", "/Studenti_Segreteria", "/Docenti_Segreteria", "/Aggiungicorso_Segreteria"})
public class Servlet_Segreteria extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
       
        
        User user = (User) request.getSession().getAttribute("user");
        
        if(user == null){
            response.sendRedirect("index.jsp?error= Prima di accedere è necessario fare il login"); 
            return ;
        }
        
        String contex = request.getRequestURI();
        
        if(contex.equals("/Sito_ingegneria/Studenti_Segreteria"))
            {
            List <Studente> lista_studenti = new ArrayList<Studente>();  
            Studente studente = new Studente();
            String nome_studente;
            String cognome_studente;
            String matricola ;
            String advisor ;
                  
            
            if(request.getParameter("ID") == null){
                nome_studente = request.getParameter("nome");
                cognome_studente = request.getParameter("cognome");
                matricola = request.getParameter("matricola");
                advisor = request.getParameter("advisor");
                    if(nome_studente != null)
                        lista_studenti = (List<Studente>) Query_Segreteria_Studenti.query_segreteria_studenti(user.getName(),nome_studente, matricola, cognome_studente, advisor);                       
            }
            
            else{
                lista_studenti = Query_Segreteria_Studenti.getTmp(user.getName());
                studente = lista_studenti.get(Integer.parseInt(request.getParameter("ID")));
            }
            
            request.setAttribute("studente", studente);
            request.setAttribute("result", lista_studenti);
            request.getRequestDispatcher("Studenti_Segreteria.jsp").forward(request, response); 
            }
        else
        if(contex.equals("/Sito_ingegneria/Corsi_Segreteria"))
            {
              List <Corso> tmp;
              Corso corso = new Corso(); 
                if(request.getParameter("ID") == null)
                    {
                       String nome = request.getParameter("corso_nome"); 
                       String crediti = request.getParameter("corso_crediti"); 
                       String docente = request.getParameter("corso_docente"); 
                       String periodo = request.getParameter("corso_periodo");
                            if((nome != null) || (crediti != null) || (docente != null) || (periodo != null))
                               tmp = Query_Segreteria_Corsi.query_lista_corsi(user.getName(), nome , crediti, docente , periodo);     
                            else
                               tmp = new ArrayList <Corso> (); 
                    }
                else
                    {
                    tmp = Query_Segreteria_Corsi.get_corsi(user.getName());
                    corso = tmp.get(Integer.parseInt(request.getParameter("ID")));         
                    }
            request.setAttribute("result", tmp);
            request.setAttribute("corso", corso);
            request.getRequestDispatcher("Corsi_Segreteria.jsp").forward(request, response);    
            }
         else
         if(contex.equals("/Sito_ingegneria/Aggiungicorso_Segreteria"))
            {

            request.getRequestDispatcher("Aggiungicorso_Segreteria.jsp").forward(request, response);  
  
         }
       }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}